define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'salary/salary_benchmark_item/index' + location.search,
                    add_url: 'salary/salary_benchmark_item/select',
                    table: 'salary_benchmark_item',
                }
            });

            var table = $("#table");

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        select: function () {
            var multiple = Backend.api.query('multiple');
            multiple = multiple === 'true';
            var staff_type = Backend.api.query('staff_type');

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'salary/salary_benchmark_item/select',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: false,
                queryParams: function (params) {
                    params['staff_type'] = staff_type;
                    return params;
                },
                columns: [
                    [
                        {field: 'state', checkbox: multiple, visible: multiple, operate: false},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'salary.title', title: '自定义基准项', operate: 'LIKE'},
                        {field: 'name', title: '单选项', operate: 'LIKE'},
                        {field: 'price', title: '费用', operate: false},
                        {
                            field: 'operate', title: __('Operate'), events: {
                                'click .btn-chooseone': function (e, value, row, index) {
                                    Fast.api.close([row]);
                                },
                            }, formatter: function () {
                                return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
                            }
                        }
                    ]
                ]
            });

            // 选中多个
            $(document).on("click", ".btn-choose-multi", function () {
                Fast.api.close(table.bootstrapTable('getOptions').selectedData);
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
